﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Services;
using PM.Entity;

namespace PM.Site.Admin
{
    public partial class TradesForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                this.Loading();
                if (Session["tradeDbKey"] != null && Session["tradeDbKey"].ToString() != string.Empty)
                {
                    this.Loading(Session["tradeDbKey"].ToString());
                }
            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            }

        }

        private void Loading()
        {
            if (!Page.IsPostBack)
            {

            }
        }

        private void Loading(string tradeKeyDb)
        {
            if (!Page.IsPostBack)
            {
                char[] seps = {','};
                string[] tradeValues = tradeKeyDb.Split(seps);

                Trade trade = TradeServices.GetTradeById(tradeValues[0], tradeValues[1], tradeValues[2], tradeValues[3]);

                txtTradeDate.Text = trade.TradeDate;
                txtTradeTime.Text = trade.TradeTime;
                ddlBuySell.SelectedValue = trade.BuySell;
                txtExecutionId.Text = trade.ExecutionId;
                txtCompanyId.Text = trade.CompanyId;
                txtMainType.Text = trade.MainType;
                txtSubType.Text = trade.SubType;
                txtQuanity.Text = trade.Quantity.ToString();
                txtPrice.Text = trade.Price.ToString();
                txtLotType.Text = trade.LotType.ToString();
                txtTaxFlag.Text = trade.TaxFlag.ToString();
                txtBrokerIdBuy.Text = trade.BrokerIdBuy;
                txtBrokerIdSell.Text = trade.BrokerIdSell;
                txtBuyingContractNo.Text = trade.BuyingContractNo.ToString();
                txtSellingContractNo.Text = trade.SellingContractNo.ToString();
                txtClientPrefix.Text = trade.ClientPrefix.ToString();
                txtClientSuffix.Text = trade.ClientSuffix;
                txtJointAcNo.Text = trade.JointAcNo.ToString();
                txtParticipantId.Text = trade.ParticipantId;
                txtForeignFlag.Text = trade.ForeignFlag.ToString();
                txtBrokerage.Text = trade.Brokerage.ToString();
                txtCdsFees.Text = trade.CdsFees.ToString();
                txtCseFees.Text = trade.CseFees.ToString();
                txtStampDuty.Text = trade.StampDuty.ToString();
                txtSecCess.Text = trade.SecCess.ToString();
                txtBtt.Text = trade.Btt.ToString();
                txtInterestOnDebt.Text = trade.InterestOnDebt.ToString();
                txtOrderId.Text = trade.OrderId;
                txtTranId.Text = trade.TranId;
                txtStatus.Text = trade.Status.ToString();
                txtGovernmentCess.Text = trade.GovernmentCess.ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int intTemp = 0;
            double dblTemp = 0;
            Trade trade = new Trade();

            trade.TradeDate = txtTradeDate.Text;
            trade.TradeTime = txtTradeTime.Text;
            trade.BuySell = ddlBuySell.SelectedValue;
            trade.ExecutionId = txtExecutionId.Text;
            trade.CompanyId = txtCompanyId.Text;
            trade.MainType = txtMainType.Text;
            trade.SubType = txtSubType.Text ;
            if (Int32.TryParse(txtQuanity.Text, out intTemp))
	        {
		        trade.Quantity = intTemp;
	        }
            if (Double.TryParse(txtPrice.Text, out dblTemp))
	        {
		        trade.Price = dblTemp;
	        }
            trade.LotType = txtLotType.Text;
            if (Int32.TryParse(txtTaxFlag.Text, out intTemp))
	        {
		        trade.TaxFlag = intTemp;
	        }
            trade.BrokerIdBuy = txtBrokerIdBuy.Text;
            trade.BrokerIdSell = txtBrokerIdSell.Text;
            if (Int32.TryParse(txtBuyingContractNo.Text, out intTemp))
	        {
		        trade.BuyingContractNo = intTemp;
	        }
            if (Int32.TryParse(txtSellingContractNo.Text, out intTemp))
	        {
		        trade.SellingContractNo = intTemp;
	        }
            if (Int32.TryParse(txtClientPrefix.Text, out intTemp))
	        {
		        trade.ClientPrefix = intTemp;
	        }
            trade.ClientSuffix = txtClientSuffix.Text;
            if (Int32.TryParse(txtJointAcNo.Text, out intTemp))
	        {
		        trade.JointAcNo = intTemp;
	        }
            trade.ParticipantId = txtParticipantId.Text;
            if (Int32.TryParse(txtForeignFlag.Text, out intTemp))
            {
                trade.ForeignFlag = intTemp;
            }
            if (Double.TryParse(txtBrokerage.Text, out dblTemp))
	        {
		        trade.Brokerage = dblTemp;
	        }
            if (Double.TryParse(txtCdsFees.Text, out dblTemp))
	        {
		        trade.CdsFees = dblTemp;
	        }
            if (Double.TryParse(txtCseFees.Text, out dblTemp))
	        {
		        trade.CseFees = dblTemp;
	        }
            if (Double.TryParse(txtStampDuty.Text, out dblTemp))
	        {
		        trade.StampDuty = dblTemp;
	        }
            if (Double.TryParse(txtSecCess.Text, out dblTemp))
	        {
		        trade.SecCess = dblTemp;
	        }
            if (Double.TryParse(txtBtt.Text, out dblTemp))
	        {
		        trade.Btt = dblTemp;
	        }
            if (Double.TryParse(txtInterestOnDebt.Text, out dblTemp))
	        {
		        trade.InterestOnDebt = dblTemp;
	        }
            trade.OrderId = txtOrderId.Text;
            trade.TranId = txtTranId.Text;
            if (Int32.TryParse(txtStatus.Text, out intTemp))
	        {
		        trade.Status = intTemp;
	        }

            if (Session["tradeDbKey"] != null )
            {
                TradeServices.Update(trade);
            }
            else
            {
                TradeServices.Add(trade);
            }
   


        }
    }
}
